package com.coder520.service.impl;

import com.coder520.common.Const;
import com.coder520.common.ServerResponse;
import com.coder520.dao.AttendMapper;
import com.coder520.dao.UserMapper;
import com.coder520.pojo.Attend;
import com.coder520.pojo.User;
import com.coder520.service.IAttendService;
import com.coder520.util.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by ASUS on 2017/7/4.
 */
@Service("iAttendService")
public class AttendServiceImpl implements IAttendService{

    @Autowired
    private AttendMapper attendMapper;

    @Autowired
    private UserMapper userMapper;

    private static Logger logger = LoggerFactory.getLogger(AttendServiceImpl.class);

    public ServerResponse toAttend(Attend attend){

        Date now = new Date();

        Date noonDate = DateUtil.getDate(Const.NOON_HOUR,Const.NOON_MINUTE);

        int week = DateUtil.getTodayWeek();

        attend.setAttendWeek((byte) week);

        attend.setAttendDate(now);

        //查询是否存在打卡记录
        Attend oldAttend = attendMapper.selectByUserId(attend.getUserId());

        User user = userMapper.selectByPrimaryKey(attend.getUserId());


        if(oldAttend==null){
            if(now.compareTo(noonDate)<=0){
                //早上打卡
                attend.setAttendMoring(now);
                int rowReslut = attendMapper.insert(attend);
                if(rowReslut>0){
                    return ServerResponse.createBySucces("打卡成功",user);
                }
            }else {
                attend.setAttendEvening(now);
                int rowReslut = attendMapper.insertSelective(attend);
                if(rowReslut>0){
                    return ServerResponse.createBySucces("打卡成功",user);
                }
            }
        }else {
            if(!(now.compareTo(noonDate)<=0)) {
                oldAttend.setAttendEvening(now);
                int rowResult = attendMapper.updateByPrimaryKeySelective(oldAttend);
                if(rowResult>0){
                    return ServerResponse.createBySucces("打卡成功",user);
                }
            }
        }
        logger.info("用户ID：{},用户姓名:{}打卡失败",user.getId(),user.getRealName());
        return ServerResponse.createByError("打卡失败");
    }

    /**
     * 按条件查询打卡记录
     */
    public ServerResponse<PageInfo> listAttend(Long userId,int pageNum,int pageSize,String rangeDate,Integer attendStatus) {

        PageHelper.startPage(pageNum, pageSize);
        String[] range = rangeDate.split("/");

        PageInfo pageInfo = null;
        List<Attend> attendList = attendMapper.selectByDate(userId, range[0], range[1],attendStatus);
        if (CollectionUtils.isNotEmpty(attendList)) {
            pageInfo = new PageInfo(attendList);
        }else {
            pageInfo = new PageInfo(null);
        }
        return ServerResponse.createBySuccess(pageInfo);

    }





}
